package xin.marcher.module.label.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.label.converter.LabelConverter;
import xin.marcher.module.label.domain.dto.LabelDTO;
import xin.marcher.module.label.domain.dto.LabelGroupDTO;
import xin.marcher.module.label.domain.entity.LabelDO;
import xin.marcher.module.label.domain.entity.LabelGroupDO;
import xin.marcher.module.label.domain.request.LabelGroupRequest;
import xin.marcher.module.label.domain.request.LabelRequest;
import xin.marcher.module.label.domain.request.QueryLabelGroupRequest;
import xin.marcher.module.label.domain.request.QueryLabelRequest;
import xin.marcher.module.label.mapper.LabelGroupMapper;
import xin.marcher.module.label.mapper.LabelMapper;

import java.util.List;
import java.util.Objects;

/**
 *
 */
@Repository
public class LabelRepository {

    @Autowired
    private LabelGroupMapper labelGroupMapper;

    @Autowired
    private LabelMapper labelMapper;

    @Autowired
    private LabelConverter labelConverter;

    public Long saveLabelGroup(LabelGroupRequest request) {
        LabelGroupDO labelGroupDO = labelConverter.convertLabelGroupDO(request);
        labelGroupDO.initCommon();
        labelGroupMapper.insert(labelGroupDO);
        return labelGroupDO.getId();
    }

    public List<LabelGroupDTO> queryLabelGroupList(QueryLabelGroupRequest request) {
        LambdaQueryWrapper<LabelGroupDO> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request.getGroupId())) {
            queryWrapper.eq(LabelGroupDO::getId, request.getGroupId());
        }
        if (StringUtils.isNotBlank(request.getGroupName())) {
            queryWrapper.like(LabelGroupDO::getGroupName, request.getGroupName());
        }
        List<LabelGroupDO> labelGroupDOS = labelGroupMapper.selectList(queryWrapper);
        return labelConverter.convertLabelGroupDOS(labelGroupDOS);
    }

    public Long saveLabel(LabelRequest request) {
        LabelDO labelDO = labelConverter.convertLabelDO(request);
        labelDO.initCommon();
        labelMapper.insert(labelDO);
        return labelDO.getId();
    }

    public PageResult<LabelDTO> queryLabelPage(QueryLabelRequest request) {
        LambdaQueryWrapper<LabelDO> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request.getId())) {
            queryWrapper.eq(LabelDO::getId, request.getId());
        }
        if (Objects.nonNull(request.getGroupId())) {
            queryWrapper.eq(LabelDO::getGroupId, request.getGroupId());
        }
        if (StringUtils.isNotBlank(request.getLabelName())) {
            queryWrapper.like(LabelDO::getLabelName, request.getLabelName());
        }
        Page<LabelDO> page = new Page<>(request.getPageNum(), request.getPageSize());
        return labelConverter.convertLabelDOPage(labelMapper.selectPage(page, queryWrapper));
    }

    public List<LabelGroupDTO> listLabelGroups(List<Long> groupIds) {
        List<LabelGroupDO> labelGroupDOS = labelGroupMapper.selectBatchIds(groupIds);
        return labelConverter.convertLabelGroupDOS(labelGroupDOS);
    }

    public List<LabelDTO> listLabelByGroupIds(List<Long> groupIds) {
        LambdaQueryWrapper<LabelDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(LabelDO::getGroupId, groupIds);
        List<LabelDO> labelDOS = labelMapper.selectList(queryWrapper);
        return labelConverter.convertLabelDOS(labelDOS);
    }

    public List<LabelDTO> listLabels(List<Long> labelIds) {
        List<LabelDO> labelDOS = labelMapper.selectBatchIds(labelIds);
        return labelConverter.convertLabelDOS(labelDOS);
    }
}
